#include "Drawer.h"

Drawer::Drawer(QWidget *parent) :
    QWidget(parent)
{
    setBackgroundColor(black);
    _myTimerId = 0;
    _xpos = 0;
    _displayDatacharge = 0;

}

void Drawer::openDraw()
{
    _close = true;
    _myTimerId = startTimer(30);
}

void Drawer::closeDraw()
{
    _close = true;
    killTimer(_myTimerId) ;
    update();
}

void Drawer::drawCurves(QPainter& painter)
{
    painter. setPen(red) ;
    _displayDatafypos= _displayDatacharge;
    _displayDatacharge = _displayDatalypos;
    painter. drawLine( -4, -_displayDatafypos, 0,  -_displayDatalypos) ;
}

void Drawer::drawGrid(QPainter& painter)
{
    painter. setPen(green);
    for (int u = 0; u < 300; ++u)
    {
        if (u%50 ==0)
        {
            painter. drawLine( -4 , u , 0 , u) ;
            painter. drawLine( -4 , -u, 0 , -u) ;
        }
    }
    if (_xpos%10 == 0 || _xpos ==0)
    {
        painter. drawLine( -4 , 500 , -4, -500);
    }
    ++_xpos;
}

void Drawer::paintEvent(QPaintEvent* event)
{
    QPainter paint( this) ;
    paint. setViewport( 0 , 0 , width() , height()) ;
    paint. setWindow(QRect(-700, -250, 701,  501));
    if (_close)
    {
        drawGrid(paint) ; //绘制网格
        drawCurves(paint) ;
    }
}

void Drawer::timerEvent(QTimerEvent* event)
{
    if (event->timerId() == _myTimerId)
    {
        scrol1(-5, 0);
        _displayDatalypos = rand()%150;
    }
    else
    {
        QWidget: :timerEvent(event);
    }
}
